package com.ruoyi.web.controller.comlbs;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Provinces;
import com.ruoyi.system.domain.UserBasicSetting;
import com.ruoyi.system.service.IUserBasicSettingService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户基础设置Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/userBasicSetting")
public class UserBasicSettingController extends BaseController {
    @Autowired
    private IUserBasicSettingService userBasicSettingService;

    /**
     * 查询用户基础设置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserBasicSetting userBasicSetting) {
        startPage();
        List<UserBasicSetting> list = userBasicSettingService.selectList(userBasicSetting);
        return getDataTable(list);
    }

    /**
     * 导出用户基础设置列表
     */
    @Log(title = "用户基础设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserBasicSetting userBasicSetting) {
        List<UserBasicSetting> list = userBasicSettingService.selectList(userBasicSetting);
        ExcelUtil<UserBasicSetting> util = new ExcelUtil<UserBasicSetting>(UserBasicSetting.class);
        util.exportExcel(response, list, "用户基础设置数据");
    }

    /**
     * 获取用户基础设置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userBasicSettingService.getById(id));
    }

    /**
     * 新增用户基础设置
     */
    @Log(title = "用户基础设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserBasicSetting userBasicSetting) {
        Long userId = getUserId();
        userBasicSetting.setUserId(userId);
        userBasicSettingService.removeByUserId(userId);
        return toAjax(userBasicSettingService.save(userBasicSetting));
    }

    /**
     * 修改用户基础设置
     */
    @Log(title = "用户基础设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserBasicSetting userBasicSetting) {
        return toAjax(userBasicSettingService.updateById(userBasicSetting));
    }

    /**
     * 删除用户基础设置
     */
    @Log(title = "用户基础设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userBasicSettingService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("查询用户基本设置信息")
    @PostMapping(value = "/getSetting")
    public AjaxResult getSetting() {
        UserBasicSetting setting = userBasicSettingService.getByUserId(getUserId());
        if (setting != null) {
            if (setting.getCityCode() != null){
                Map<String, Object> cityCodeMap = userBasicSettingService.selectParentCode(setting.getCityCode()).stream().collect(
                        Collectors.toMap(item -> "cityCode" + item.getDepth(), Provinces::getCityCode));
                setting.setCityCodeMap(cityCodeMap);
            }
            return success(setting);
        } else {
            return success(new UserBasicSetting());
        }
    }
}
